<?php
try {
    /*****************************/
    /* Chargement des constantes */
    /*****************************/
    require_once 'config/config.php';
    require_once 'config/card_collection.php';

    require_once 'controler/lib/codeRetour.fct.php';
    require_once 'controler/lib/validParam.fct.php';
    require_once 'controler/lib/cache.fct.php';
    require_once 'controler/lib/game.fct.php';
    require_once 'controler/lib/log.fct.php';

    /***********************************/
    /* Tableau de jointure des actions */
    /***********************************/
    $operationListJoin = array('loadBookPage'      => 'loadBookPage.php'
    						   ,'refresh'          => 'refresh.php'
    						   ,'play'             => 'play.php'
    						   ,'init'	           => 'init.php'
    						   ,'card'    		   => 'card.php'
    						   ,'release'          => 'release.php'
	    					   ,'changePlayerId'   => 'admin/changePlayerId.php'
	    					   ,'changeGameId'     => 'admin/changeGameId.php'
	    					   ,'admin'   	  	   => 'admin/index.php'
	    					   ,'purge'   		   => 'admin/purge.php'
	    					   ,'viewGame'   	   => 'admin/viewGame.php'
	    					   ,'viewGames'   	   => 'admin/viewGames.php'
	    					   ,'viewPlayer'   	   => 'admin/viewPlayer.php'
	    					   ,'viewPlayers'      => 'admin/viewPlayers.php'
	    					   ,'delete'     	   => 'admin/delete.php'
    						  );

    /**************************/
    /* Gestion des paramètres */
    /**************************/
    // Validation
    $operation = validGET('_operation');

    /*****************************/
    /* Gestion de l'url demandée */
    /*****************************/
    if (in_array($operation, array_keys($operationListJoin))){
        // Chargement du fichier de l'opération
        require_once sprintf('controler/%s', $operationListJoin[$operation]);
    }else{
        throw new Exception(sprintf('Opération %s non trouvée.', $operation), HTTP_NOTIMPLEMENTED);
    }
} catch(Exception $e) {
	/********************/
    /* Gestion des logs */
    /********************/
    // Ouverture du fichier
    $file=sprintf('%s/log/%s.technique.log', dirname(__FILE__),date('d-m-y'));
    $fp = fopen($file,'a+');
	// Ajout des logs
    fputs($fp, sprintf("[%s] %s\n"
    				  ,date('H:i:s')
    				  ,$e->getMessage()
                      ));
    // Fermeture
    fclose($fp);
    /********************/
    /* Envois du header */
    /********************/
    HTTPStatus($e->getCode());
	echo json_encode(array('error'=>0, 'msg' => $e->getMessage()));
}